package cn.stylefeng.guns.sys.core.listener;

import cn.stylefeng.guns.base.auth.context.LoginContextHolder;
import cn.stylefeng.guns.sys.core.util.UUIDUtils;
import cn.stylefeng.guns.sys.modular.system.entity.BtyMtsxf;
import cn.stylefeng.guns.sys.modular.system.entity.YaoluBankFlow;
import cn.stylefeng.guns.sys.modular.system.service.BtyMtsxfService;
import cn.stylefeng.guns.sys.modular.system.service.YaoluBankFlowService;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/*
 * 监听器批处理数据
 * */
public class YaoluBankFlowExcelListener extends AnalysisEventListener<YaoluBankFlow> {

    private YaoluBankFlowService bankFlowService;

    public YaoluBankFlowExcelListener(){

    }

    public YaoluBankFlowExcelListener(YaoluBankFlowService bankFlowService){
        this.bankFlowService = bankFlowService;
    }


    /**
     * 批处理阈值2000
     */
    private static final int BATCH_COUNT = 300;
    List<YaoluBankFlow> list = new ArrayList<>();

    /**
     *
     * @param basic
     * @param analysisContext
     */
    @Override
    public void invoke(YaoluBankFlow basic, AnalysisContext analysisContext) {
       // String mdid=basic.getMdid().replaceAll("\\s*", "");
//        basic.setMdid(basic.getMdid().split("\\.")[0]);
//        basic.setBh(basic.getBh().split("\\.")[0]);
        basic.setCreateTime(new Date());
        basic.setCreateUser(LoginContextHolder.getContext().getUserId());
        list.add(basic);
        if (list.size() >= BATCH_COUNT) {
            saveData();
            list.clear();
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        analysisContext.readWorkbookHolder().setDefaultReturnMap(false);
        saveData();
        // log.info("所有数据解析完成！");

    }
    private void saveData(){
        //调用saveData()方法
        bankFlowService.insertYaoluBankFlow(list);
    }
}